package com.leetcode.LC_MED;

public class LC209 {
    public int minSubArrayLen(int target, int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int i = 0, j = 0;
        int total = 0, result = nums.length + 1;

        while (j < nums.length) {
            total += nums[j];
            j++;
            while (total >= target) {
                result = Math.min(j - i, result);
                total -= nums[i];
                i++;
            }
        }
        return result == nums.length + 1 ? 0 : result;
    }

    public int minSubArrayLen_2(int target, int[] nums) {
        int left = 0, right = 0, sum = 0, min = Integer.MAX_VALUE;
        for (right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                min = Math.min(min, right - left + 1);
                sum -= nums[left];
                left++;
            }
        }
        return min == Integer.MAX_VALUE ? 0 : min;
    }
}
